package com.tech.koufu.ui.view;

import com.tech.koufu.MainActivity;
import com.tech.koufu.MyApplication;
import com.tech.koufu.R;
import com.tech.koufu.tools.PublicFuc;
import com.tech.koufu.tools.Statics;
import com.umeng.analytics.MobclickAgent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 首页fragment容器，用来包含登录fragment和登录后用户的首页的fragment
 * @author 123
 *
 */
public class HomeContainerFragment extends BaseContainerFragment{
	private static final String TAG = HomeContainerFragment.class.getName();
	private Context mContext;
	 private boolean mIsViewInited;
	 private boolean isLogin = false;
	 MyApplication myApp ;
	private View view;
	private Fragment contentFrg,loginFrg;
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
//         return inflater.inflate(R.layout.container_fragment, container,false);
         if (view == null) {  Log.d(TAG, "HomeContainerFragment~~1~~~");
 			view = inflater.inflate(R.layout.container_fragment, container,
 					false); 
 		   if (!mIsViewInited) {
	             mIsViewInited = true;
	             initView();
	         }
 		} else {  Log.d(TAG, "HomeContainerFragment~~2~~");
 		} 
 		ViewGroup parent = (ViewGroup) view.getParent();
 		if (parent != null) {
 			parent.removeView(view);
 		}
 		mContext = this.getActivity();
 		return view;
     }

	 @Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(mContext);
	}
	 
	 @Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(mContext);
	}
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
//         Log.e("Tt", "tab 1 container on activity created"); 
//         if (!mIsViewInited) {
//             mIsViewInited = true;
//             initView();
//         }
     }

     private void initView() {
    	 myApp = (MyApplication) getActivity().getApplication();
    	 isLogin = myApp.isLogin();
    	 Log.v(TAG, String.valueOf(isLogin)); 
         if(isLogin){
        	 if(contentFrg==null){
        		 contentFrg = MainContentFragment.getInstance(Statics.TAG_Content);
        	 }
        	 replaceFragment(contentFrg, false,Statics.TAG_Content); 
         }else{
        	 if(loginFrg == null){
        		 loginFrg = new MainLoginFragment();
        	 }
        	 replaceFragment(loginFrg, false,Statics.TAG_Login); 
         }
        
     }
     
     @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    	super.onActivityResult(requestCode, resultCode, data);
    	Log.d(TAG, "~~~~onActivityResult，HomeContainerFragment~~~"+myApp.isLogin); 
    	Log.d(TAG, "~~~~onActivityResult，HomeContainerFragment~~~"
    			+ "requestCode="+requestCode+",resultCode="+resultCode);
//    	myApp.isLogin = true;
//    	isLogin = false; 
    	myApp = (MyApplication) getActivity().getApplication();
    	Log.d(TAG, "~~2~~onActivityResult，HomeContainerFragment~~~"+myApp.isLogin); 
    	if(myApp.isLogin && resultCode==getActivity().RESULT_OK && requestCode==PublicFuc.code_login_suss){  
    		replaceFragment(MainContentFragment.getInstance(Statics.TAG_Content), false,Statics.TAG_Content);
    		 MainActivity mainActivity = (MainActivity) getActivity();
    		 mainActivity.showLeftMenu();
    	}else if(requestCode == PublicFuc.CODE_Back && resultCode == PublicFuc.CODE_Back){
    		changeFragment();
    		MainActivity mainActivity = (MainActivity) getActivity();
    		mainActivity.showLeftMenu();
    	}
    }
     public void refreshContent(){Log.d(TAG, "~~~~refreshContent~~~~~~HomeContainerFragment~~~"); 
    	 Fragment currentFragment = getChildFragmentManager().findFragmentByTag(Statics.TAG_Content); 
    	 if(currentFragment!=null && currentFragment instanceof MainContentFragment){ 
    		 MainContentFragment mcf  =  (MainContentFragment) currentFragment;
    		 mcf.refreshData();
    	 }
     }
     public void changeFragment(){
    	 Fragment currentFragment = getChildFragmentManager().findFragmentByTag(Statics.TAG_Content); 
    	 if(currentFragment!=null && currentFragment instanceof MainContentFragment){ 
    		 if(loginFrg == null){
        		 loginFrg = new MainLoginFragment();
        	 }
        	 replaceFragment(loginFrg, false,Statics.TAG_Login); 
    	 }
     }
     public void changeContentFragment(){
    	 Fragment currentFragment = getChildFragmentManager().findFragmentByTag(Statics.TAG_Login); 
    	 if(currentFragment!=null && currentFragment instanceof MainLoginFragment){ 
    		 if(contentFrg == null){
    			 contentFrg = new MainContentFragment();
        	 }
    		 replaceFragment(contentFrg, false,Statics.TAG_Content); 
    	 }
     }
}
